package helper

import (
	"golang.org/x/crypto/ssh"
	"io"
	"log"
	"net"
)

// 设置跳板机，包括用户名密码之类的
func SetClientConfig(username string, password string) (config *ssh.ClientConfig) {
	config = &ssh.ClientConfig{
		User: username,
		Auth: []ssh.AuthMethod{
			ssh.Password(password),
		},
		HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
			return nil
		},
	}
	return
}

// 建立两个连接的通道，即互相拷贝实现转发
func CopyConn(connLeft net.Conn, connRight net.Conn) {
	_, err := io.Copy(connLeft, connRight)
	if err != nil {
		log.Fatalf("io.Copy failed: %v", err)
	}
}

// 打开ssh
func OpenClient(clientAddr string, clientConfig *ssh.ClientConfig) (client *ssh.Client) {
	client, err := ssh.Dial("tcp", clientAddr, clientConfig)
	if err != nil {
		log.Fatalf("ssh.Dial failed: %s", err)
	}
	return
}
